//
//  LDVistorView.swift
//  Weibo201655
//
//  Created by pro on 16/5/5.
//  Copyright © 2016年 LeeDan. All rights reserved.
//

import UIKit

@objc
protocol LDVistorViewDelegate: NSObjectProtocol {
    optional func vistorViewRegisterButtonClick()
    func vistorViewLoginButtonClick()
}

class LDVistorView: UIView {
    
    //MARK: 代理属性
    weak var vistorViewDelegate: LDVistorViewDelegate?

    //MARK: 判断是否显示转轮，并且每个页面显示不一样的图片和文字
    func showVistorViewInfo(isHome: Bool, imageName: String, title: String) {
        
        if isHome {
            homeImgView.hidden = false
            coverView.hidden = false
            //开始动画
            rotateAnimationInHome()
        } else {
            homeImgView.hidden = true
            coverView.hidden = true
        }
        iconView.image = UIImage(named: imageName)
        messageLabel.text = title
    }
    
    //MARK: 动画
    func rotateAnimationInHome() {
        let basicAni = CABasicAnimation(keyPath: "transform.rotation")
        
        basicAni.toValue = 2 * M_PI
        basicAni.repeatCount = MAXFLOAT
        basicAni.duration = 20
        // 动画完成的时候不要移除,不然切换到其它界面在切换回来就不会动画了
        basicAni.removedOnCompletion = false
        
        iconView.layer.addAnimation(basicAni, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //背景色
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        setupUI()
    }

    // 从xib/storyboard里面加载会走这个方法
    // 当子类实现init(frame: CGRect)就不会继承父类的构造函数init?(coder aDecoder: NSCoder),就不能从xib/storyboard里面加载
    // 只有同时实现init(frame: CGRect)和init?(coder aDecoder: NSCoder)才能保证view即可以使用代码创建,也可以使用xib/storyboar来创建
    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    
    func setupUI() {
        
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeImgView)
        
        addSubview(messageLabel)
        
        addSubview(registerBtn)
        addSubview(LoginBtn)
        
        //添加约束
        setupConstraint()
    }

    func setupConstraint() {
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeImgView.translatesAutoresizingMaskIntoConstraints = false
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        LoginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //iconView
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40.0))
        //coverView
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
        //homeImgView
        addConstraint(NSLayoutConstraint(item: homeImgView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: homeImgView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0.0))
        //messageLabel
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 240))
        // registerBtn
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        //宽度 高度
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 35))
        // LoginBtn
        addConstraint(NSLayoutConstraint(item: LoginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: LoginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        //宽度 高度
        addConstraint(NSLayoutConstraint(item: LoginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: LoginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 35))
        
    }
    
    //MARK: 按钮点击事件
    func registerBtnClick() {
        
        vistorViewDelegate?.vistorViewRegisterButtonClick!()
    }
    
    func loginBtnClick() {
        vistorViewDelegate?.vistorViewLoginButtonClick()
    }
    
    //MARK: 懒加载
    //转轮
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //房子
    lazy var homeImgView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 消息Label
    lazy var messageLabel: UILabel = {
       
        let label = UILabel()
        label.text = "关注一些人,看看有上面惊喜"
        label.font = UIFont.systemFontOfSize(18)
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
        
        return label
        
    }()
    
    // 注册按钮
    lazy var registerBtn: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    // 登录按钮
    lazy var LoginBtn: UIButton = {
        let btn = UIButton()
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
     // 遮盖
    lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}
